package cn.exrick.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.exrick.pojo.TbItemSkuCat;
import cn.exrick.pojo.TbItemSkuCatAttr;
import cn.exrick.service.ItemSkuCatAttrService;
import cn.exrick.service.ItemSkuCatService;
import cn.exrick.utils.mybatis.Page;
import cn.exrick.utils.mybatis.PageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(description = "SKU管理")
public class SkuController {
    @Autowired
    ItemSkuCatService itemSkuCatService;
    @Autowired
    ItemSkuCatAttrService itemSkuCatAttrService;

    @RequestMapping(value = "/sys/sku/cat/list", method = RequestMethod.GET)
    @ApiOperation(value = "获取SKU分类列表(一级)")
    public Page<TbItemSkuCat> getSkuCatList(String name, Integer pageNo, Integer pageSize) throws Exception {
        if (pageNo == null || pageNo < 0) {
            pageNo = 0;
        }
        if (pageSize == null || pageSize < 0) {
            pageSize = 10;
        }
        PageParam pageParam = new PageParam(pageNo, pageSize);
        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isNotEmpty(name)) {
            params.put("name", name);
        }
        Page<TbItemSkuCat> page = itemSkuCatService.findPageByCriteria(params, pageParam);
        return page;
    }

    @RequestMapping(value = "/sys/sku/cat/list", method = RequestMethod.POST)
    @ApiOperation(value = "获取SKU分类列表(一级) --- 仅list,无分页")
    public List<TbItemSkuCat> getSkuCatList() throws Exception {
        List<TbItemSkuCat> list = itemSkuCatService.findListByCriteria(new HashMap<>());
        for(TbItemSkuCat itemSkuCat:list){
            List<TbItemSkuCatAttr> itemSkuCatAttrs = itemSkuCatAttrService.findByCatId(itemSkuCat.getId());
            itemSkuCat.setItemSkuCatAttrs(itemSkuCatAttrs);
        }
        return list;
    }

    @RequestMapping(value = "/sys/sku/cat/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "获取SKU分类列表(一级)")
    public TbItemSkuCat getSkuCat(@PathVariable Long id) throws Exception {
        if (id == null) {
            throw new Exception("参数不足");
        }
        return itemSkuCatService.findById(id);
    }

    @RequestMapping(value = "/sys/sku/cat/add", method = RequestMethod.POST)
    @ApiOperation(value = "新增SKU分类列表(一级)")
    public Map<String, Object> addSkuCat(String name) throws Exception {
        if (itemSkuCatService.exist(name)) {
            throw new Exception("sku分类已存在");
        }
        TbItemSkuCat tbItemSkuCat = new TbItemSkuCat();
        tbItemSkuCat.setName(name);
        itemSkuCatService.save(tbItemSkuCat);
        return new HashMap<String, Object>() {{
            this.put("success", 1);
        }};
    }

    @RequestMapping(value = "/sys/sku/cat/update/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "新增SKU分类列表(一级)")
    public Map<String, Object> updateSkuCat(@PathVariable Long id, String name) throws Exception {
        if (id == null) {
            throw new Exception("请输入id");
        }
        TbItemSkuCat tbItemSkuCat = itemSkuCatService.findById(id);
        if (tbItemSkuCat == null) {
            throw new Exception("sku分类不存在");
        }
        if (itemSkuCatService.exist(name)) {
            throw new Exception("sku分类名称已被使用");
        }
        tbItemSkuCat.setName(name);
        itemSkuCatService.update(tbItemSkuCat);
        return new HashMap<String, Object>() {{
            this.put("success", 1);
        }};
    }


    //=====================================================cat_attr
    @RequestMapping(value = "/sys/sku/catAttr/list", method = RequestMethod.GET)
    @ApiOperation(value = "获取SKU分类列表(二级)")
    public Page<TbItemSkuCatAttr> getSkuCatAttrList(Long catId, String name, Integer pageNo, Integer pageSize) throws Exception {
        if (pageNo == null || pageNo < 0) {
            pageNo = 0;
        }
        if (pageSize == null || pageSize < 0) {
            pageSize = 10;
        }
        PageParam pageParam = new PageParam(pageNo, pageSize);
        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isNotEmpty(name)) {
            params.put("name", name);
        }
        if (catId != null) {
            params.put("catId", catId);
        }
        Page<TbItemSkuCatAttr> page = itemSkuCatAttrService.findPageByCriteria(params, pageParam);
        return page;
    }

    @RequestMapping(value = "/sys/sku/catAttr/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "获取SKU分类列表(二级)")
    public TbItemSkuCatAttr getSkuCatAttr(@PathVariable Long id) throws Exception {
        if (id == null) {
            throw new Exception("参数不足");
        }
        TbItemSkuCatAttr itemSkuCatAttr = itemSkuCatAttrService.findById(id);
        if (itemSkuCatAttr != null && itemSkuCatAttr.getItemSkuCatId() != null) {
            TbItemSkuCat itemSkuCat = itemSkuCatService.findById(itemSkuCatAttr.getItemSkuCatId());
            if (itemSkuCat != null) {
                itemSkuCatAttr.setItemSkuCatName(itemSkuCat.getName());
            }
        }
        return itemSkuCatAttr;
    }

    @RequestMapping(value = "/sys/sku/catAttr/add", method = RequestMethod.POST)
    @ApiOperation(value = "新增SKU分类列表(二级)")
    public Map<String, Object> addSkuCatAttr(Long catId, String name) throws Exception {
        if (catId == null || StringUtils.isEmpty(name)) {
            throw new Exception("参数错误");
        }
        if (itemSkuCatService.findById(catId) == null) {
            throw new Exception("sku一级分类不存在");
        }
        if (itemSkuCatAttrService.exist(catId, name)) {
            throw new Exception("sku二级分类已存在");
        }
        TbItemSkuCatAttr tbItemSkuCatAttr = new TbItemSkuCatAttr();
        tbItemSkuCatAttr.setItemSkuCatId(catId);
        tbItemSkuCatAttr.setName(name);
        itemSkuCatAttrService.save(tbItemSkuCatAttr);
        return new HashMap<String, Object>() {{
            this.put("success", 1);
        }};
    }

    @RequestMapping(value = "/sys/sku/catAttr/update/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "更新SKU分类列表(二级)")
    public Map<String, Object> updateSkuCatAttr(@PathVariable Long id, String name) throws Exception {
        if (id == null) {
            throw new Exception("请输入id");
        }
        TbItemSkuCatAttr tbItemSkuCatAttr = itemSkuCatAttrService.findById(id);
        if (tbItemSkuCatAttr == null) {
            throw new Exception("sku二级分类不存在");
        }
        if (itemSkuCatAttrService.exist(tbItemSkuCatAttr.getItemSkuCatId(), name)) {
            throw new Exception("sku二级分类名称已被使用");
        }
        tbItemSkuCatAttr.setName(name);
        itemSkuCatAttrService.update(tbItemSkuCatAttr);
        return new HashMap<String, Object>() {{
            this.put("success", 1);
        }};
    }

    @RequestMapping(value = "/sys/sku/catAttr/delete/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "删除SKU分类列表(二级)")
    public Map<String, Object> deleteSkuCatAttr(@PathVariable String id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请输入id");
        }
        List<Long> ids = null;
        try {
            String[] idstrs = id.split(",");
            ids = new ArrayList<>();
            for (String idstr : idstrs) {
                long i = Long.parseLong(idstr);
                TbItemSkuCatAttr tbItemSkuCatAttr = itemSkuCatAttrService.findById(i);
                if (tbItemSkuCatAttr == null) {
                    throw new Exception("sku二级分类不存在");
                }
                ids.add(i);
            }
        }catch (Exception e){
            throw new Exception("id不合法");
        }
        for(Long i:ids){
            itemSkuCatAttrService.delete(i);
        }
        return new HashMap<String, Object>() {{
            this.put("success", 1);
        }};
    }
}
